from django import forms
from user import models
import re


# 用户注册Form组件
class RegForm(forms.Form):
    username = forms.CharField(label= '用户名',min_length= 3,max_length= 16,
                               error_messages= {
                                   'min_length': '用户名最少3个字符',
                                   'max_length': '用户名最多16个字符',
                                   'required': '用户名不能为空'
                               },
                               widget= forms.widgets.TextInput(
                                   attrs={'class': 'form-control'})
                               )

    password = forms.CharField(label= '密码', min_length= 6, max_length= 16,
                               error_messages={
                                   'min_length': '密码最少6个字符',
                                   'max_length': '密码最多16个字符',
                                   'required': '密码不能为空'
                               },
                               widget= forms.widgets.PasswordInput(
                                   attrs= {'class': 'form-control'})
                               )

    confirm_password = forms.CharField(label= '确认密码', min_length= 6, max_length= 16,
                                       error_messages={
                                           'min_length': '密码最少6个字符',
                                           'max_length': '密码最多16个字符',
                                           'required': '密码不能为空'
                                       },
                                       widget=forms.widgets.PasswordInput(
                                           attrs={'class': 'form-control'})
                                       )

    email = forms.CharField(label= '邮箱',
                            error_messages={
                                'requird': '邮箱不能为空，需通过邮箱激活用户',
                                'invalid': '邮箱格式不正确'
                            },
                            widget= forms.widgets.EmailInput(
                                attrs= {'class': 'form-control'})
                            )

    telephone = forms.CharField(label='手机号码',
                                error_messages={
                                    'required': '手机号码不能为空'
                                },
                                widget= forms.widgets.TextInput(
                                    attrs= {'class': 'form-control'})
                                )


    def clean_email(self):
        '''
        校验邮箱是否已注册
        '''
        email = self.cleaned_data.get('email')
        user_obj = models.User.objects.filter(email= email)

        if user_obj:
            self.add_error('email', '邮箱已注册')

        # 判断邮箱是否合法
        pattern = '[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z0-9-.]'
        if not re.match(pattern, email):
            self.add_error('email', '邮箱格式不合法')

        return email

    def clean_telephone(self):
        '''校验手机号是否已注册'''
        telephone = self.cleaned_data.get('telephone')
        user_obj = models.User.objects.filter(telephone= telephone)

        if user_obj:
            self.add_error('telephone', '手机号码已注册')

        # 校验手机号码是否合法
        if not re.match('^1[3-9][0-9]{9}$', telephone):
            self.add_error('telephone', '手机号码不合法')

        return telephone


    def clean(self):
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')

        if not password == confirm_password:
            self.add_error('confirm_password', '两次密码不一致')

        return self.cleaned_data

